import i18n from '../../../i18n';

const Storage = storage.getDataStorage('Storage_players');
/**
 * 排行API
 */
class rankAPI {
  /**
   * 获取玩家排行(从大到小排序)
   */
  static async bigBroad(
    type: 'coin' | 'fastestRecord' | 'level',
    entity: GamePlayerEntity
  ) {
    const rankList: { name: string; value: number }[] = [];
    const allData = await Storage.list({
      cursor: 0,
      pageSize: 100,
    });
    let number = 100;
    while (number) {
      const currentPage = allData.getCurrentPage();
      currentPage.forEach((x) => {
        rankList.push({
          name: x?.value['name' as keyof JSONValue] ?? '',
          value: x?.value[type as keyof JSONValue] ?? 0,
        });
      });
      number--;
      if (number === 0) {
        break;
      }
      if (allData.isLastPage) {
        break;
      }
      await allData.nextPage();
    }
    rankList.sort((a, b) => b.value - a.value);
    return rankList;
  }
  /**
   * 获取玩家排行(从小到大排序)
   */
  static async littleBroad(
    type: 'coin' | 'fastestRecord' | 'level',
    entity: GamePlayerEntity
  ) {
    const rankList: { name: string; value: number }[] = [];
    const allData = await Storage.list({
      cursor: 0,
      pageSize: 100,
    });
    let number = 100;
    while (number) {
      const currentPage = allData.getCurrentPage();
      currentPage.forEach((x) => {
        rankList.push({
          name: x?.value['name' as keyof JSONValue] ?? '',
          value: x?.value[type as keyof JSONValue] ?? 0,
        });
      });
      number--;
      if (number === 0) {
        break;
      }
      if (allData.isLastPage) {
        break;
      }
      await allData.nextPage();
    }
    rankList.sort((a, b) => a.value - b.value);
    return rankList;
  }
  /**
   * 获取排行文本
   */
  static async getRankText(
    type: 'coin' | 'fastestRecord' | 'level',
    entity: GamePlayerEntity
  ) {
    let content = '';
    let rankList: { name: string; value: number }[] = [];
    if (type === 'coin') {
      rankList = await rankAPI.bigBroad(type, entity);
    } else if (type === 'fastestRecord') {
      rankList = await rankAPI.littleBroad(type, entity);
    } else if (type === 'level') {
      rankList = await rankAPI.bigBroad(type, entity);
    }
    for (let i = 0; i < rankList.length; i++) {
      content += `${i18n.t('rank.content', {
        lng: entity.language,
        number: (i + 1).toString(),
        name: rankList[i].name,
        type: i18n.t(
          `rank.${type}_rank` as
            | 'rank.coin_rank'
            | 'rank.fastestRecord_rank'
            | 'rank.level_rank',
          {
            lng: entity.language,
          }
        ),
        value: rankList[i].value.toString(),
      })}\n`;
    }
    return content;
  }
}
const rank = world.querySelector('#rank') as GameEntity;
rank.enableInteract = true;
rank.interactRadius = 5;
rank.onInteract(async ({ entity }) => {
  const d1 = await entity.player.dialog<GameSelectDialogParams>({
    type: GameDialogType.SELECT,
    title: i18n.t('rank.title', {
      lng: entity.language,
    }),
    content: i18n.t('rank.select', {
      lng: entity.language,
    }),
    options: [
      i18n.t('rank.coin_rank', {
        lng: entity.language,
      }),
      i18n.t('rank.fastestRecord_rank', {
        lng: entity.language,
      }),
      i18n.t('rank.level_rank', {
        lng: entity.language,
      }),
    ],
  });
  if (d1 === null) {
    return;
  }
  let rankText = '';
  if (d1.index === 0) {
    rankText = await rankAPI.getRankText('coin', entity);
  } else if (d1.index === 1) {
    rankText = await rankAPI.getRankText('fastestRecord', entity);
  } else if (d1.index === 2) {
    rankText = await rankAPI.getRankText('level', entity);
  }
  await entity.player.dialog<GameTextDialogParams>({
    type: GameDialogType.TEXT,
    title: i18n.t('rank.title', {
      lng: entity.language,
    }),
    content: rankText,
  });
});
